package com.example.zbh.dsliao.ui.adapter;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.zbh.dsliao.R;
import com.example.zbh.dsliao.model.entity.SearchSinger;
import com.example.zbh.dsliao.model.entity.SearchSong;
import com.example.zbh.dsliao.ui.fragment.SearchFragment;

import java.util.List;

/**
 * Created by yaolin on 15/11/27.
 */
public class SearchAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<SearchSong> songList;
    private List<SearchSinger> singerList;
    private static final int TYPESONG = 0;
    private static final int TYPESINGER = 1;
    private setSongOnClickListerner songListerner;
    private setSignerOnClickListerner singerListener;


    /**歌名行监听**/
    public void setSongListerner(setSongOnClickListerner listerner){
        this.songListerner = listerner;
    }


    /**歌手行监听**/
    public void setSignerListener(setSignerOnClickListerner signerListener){
        this.singerListener = signerListener;
    }




    public interface setSongOnClickListerner{
        void onSongItemCick(List<SearchSong> song, int position);
    }

    public interface  setSignerOnClickListerner{
        void onSignerItemClick(SearchSinger singer);
    }

    /**添加数据**/
    public void addData(List<SearchSong> songList ){
        this.singerList = singerList;
        this.songList = songList;
        notifyDataSetChanged();

    }
    public void addOtherData(List<SearchSinger> singerList){
        this.singerList = singerList;
        notifyDataSetChanged();

    }


    @Override
    public int getItemViewType(int position) {
        if (isSongLength() && position  < songList.size()){
            return TYPESONG;
        }else {
            return TYPESINGER;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        switch (viewType){
            case TYPESONG:
                View songView = inflater.inflate(R.layout.search_song_list, null);
                return new SongViewHolder(songView);
            case TYPESINGER:
                View singerView = inflater.inflate(R.layout.search_singer_list, null);
                return new SingerViewHolder(singerView);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        if (holder instanceof  SongViewHolder){
            if (isSongLength()){
                SearchSong song = songList.get(position);
                if (song != null){
                ((SongViewHolder) holder).signer_tv.setText(song.getSinger_name());
                ((SongViewHolder) holder).songTv.setText(song.getName());
                ((SongViewHolder) holder).songPosition  = position;
                }
            }

        }else if (holder instanceof SingerViewHolder) {
            Log.d("SearchAdapter", "position:" + position + "");
            if (isSingerLength()){
                SearchSinger singer = singerList.get(position - (isSongLength() ?songList.size() : 0));
                ((SingerViewHolder) holder).signerTv.setText(singer.getSinger_name() + "(" + singer.getAlias_name() + ")");
                ((SingerViewHolder) holder).singerPosition = position;
            }
        }

    }

    private boolean isSongLength(){
        return songList != null && songList.size() > 0 ;
    }
    private boolean isSingerLength(){
        return singerList != null && singerList.size() > 0;
    }

    @Override
    public int getItemCount() {
        if (isSingerLength() && isSongLength()) {
            return singerList.size() + songList.size();
        } else {
            if (isSongLength()) {
                return songList.size();
            } else if (isSingerLength()) {
                return singerList.size();
            } else {
                return 0;
            }
        }

    }


    class SongViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView signer_tv, songTv;
        private LinearLayout songLayout;

        private int songPosition;

        public SongViewHolder(View itemView) {
            super(itemView);
            signer_tv = (TextView) itemView.findViewById(R.id.signer_tv);
            songTv = (TextView) itemView.findViewById(R.id.songTv);
            songLayout = (LinearLayout) itemView.findViewById(R.id.songLayout);
            songLayout.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if(isSongLength()){
                SearchSong song = songList.get(songPosition);
                if(songListerner != null){
                    songListerner.onSongItemCick(songList, songPosition);
                }
            }

        }
    }




    class SingerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView signerTv;
        private LinearLayout signerLayout;

        private int singerPosition;

        public SingerViewHolder(View itemView) {
            super(itemView);

            signerTv = (TextView) itemView.findViewById(R.id.signerTv);
            signerLayout = (LinearLayout) itemView.findViewById(R.id.signerLayout);
            signerLayout.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if(isSingerLength()){
                SearchSinger singer = singerList.get(singerPosition - (isSongLength() ?songList.size() : 0));
                if(singerListener != null){
                    singerListener.onSignerItemClick(singer);
                }
            }
        }
    }

}
